import React,{ createContext, useReducer } from 'react';

export const ColorContext = createContext({});

export const UPDATE_COLOR = 'UPDATE_COLOR';

const params = {
    name:'mic',
    age:22,
    content:'hello',
}

const reducer = (state, action) => {
    switch (action.type) {
        case UPDATE_COLOR:
            return action.color;
        default:
            return state
    }
}

export const Color = props => {
    const [color, dispatch] = useReducer(reducer,'red'); // 第一个参数时用于逻辑判断的reducer函数，第二个参数是color的默认值

    return (
        <ColorContext.Provider value={{...params,color,dispatch}}>
            {props.children}
        </ColorContext.Provider>
    )
}

